// Copyright (C) 2021  The GoHBase Authors.  All rights reserved.
// This file is part of GoHBase.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.

package gohbase

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
	operationDurationSeconds = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "gohbase",
			Name:      "operation_duration_seconds",
			Help:      "Time in seconds for operation to complete",
			//   >>> [0.04*(2**i) for i in range(11)]
			//   [0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24, 20.48, 40.96]
			// Meaning 40ms, 80ms, 160ms, 320ms, 640ms, 1.28s, ... max 40.96s
			// (most requests have a 30s timeout by default at the Envoy level)
			Buckets: prometheus.ExponentialBuckets(0.04, 2, 11),
		},
		[]string{"operation", "result"},
	)

	sendBatchSplitCount = promauto.NewHistogram(
		prometheus.HistogramOpts{
			Namespace: "gohbase",
			Name:      "sendbatch_split_count",
			Help:      "Count of Region Servers hit per SendBatch",
			// 1, 2, 4, 8, 16, 32, 64, 128, 256, 512
			Buckets: prometheus.ExponentialBuckets(1, 2, 10),
		},
	)

	cachedRegionTotal = promauto.NewGauge(prometheus.GaugeOpts{
		Namespace: "gohbase",
		Subsystem: "cache",
		Name:      "regions_total",
		Help:      "Total number of regions in the cache",
	})

	retryBackoffDuration = promauto.NewHistogram(prometheus.HistogramOpts{
		Namespace: "gohbase",
		Subsystem: "retry",
		Name:      "backoff_duration_seconds",
		Help:      "Time spend sleeping in retry backoff",
		// Buckets match the exact backoff time generated by the sleepAndIncreaseBackoff function
		Buckets: []float64{
			0.016, 0.032, 0.064, 0.128, 0.256, 0.512, 1.024, 2.048,
			4.096, 8.192, 13.192, 18.192, 23.192, 28.192, 33.192,
		},
	})

	scanRenewers = promauto.NewGauge(prometheus.GaugeOpts{
		Namespace: "gohbase",
		Subsystem: "scanner",
		Name:      "renewer_count",
		Help: "Number of active scanner renewers. " +
			"A continually increasing value indicates an Scanner leak.",
	})
)
